﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EmotionRecognitionEntities;
using EmotionRecognitionMessages;

namespace EmotionRecognitionGui
{
    public partial class UpdateLastImages : BasicForm
    {//

        int i = 0;
        int firstpictureID = 0;
        GuiManager guiManager;
        Picture currentDisplayedPicture;

        Dictionary<int, Picture> newPictureDictionary;

        Messages retrievalmsg = new Messages();

        
        public UpdateLastImages()
        {
            try
            {
                guiManager = GuiManager.Instance;
                InitializeComponent();
            }
            catch (Exception e)
            {
                MessageBox.Show("Error: " + e.ToString());
            }
        }
        //the function when reentering this GUI
        public void ReEnter()
        {
            ShowFirstImage();
        }

        public void ShowFirstImage()
        {
            fiilAUInsertListBox();

            ReplyMsg reply = retrievalmsg.LoadPicutersFromDB(2);
            if (reply.MessageState)
                newPictureDictionary = (Dictionary<int, Picture>)reply.ParamArray[0];
            if (newPictureDictionary.Count != 0)
            {
                EnableButtons();
                pictureBoxImage.Image = newPictureDictionary.Values.First().PictureImage;
                currentDisplayedPicture = newPictureDictionary.Values.First();
                pictureBoxImage.SizeMode = PictureBoxSizeMode.Zoom;
                // showCurrentAusinListbox();
                firstpictureID = currentDisplayedPicture.PictureID;
                i = firstpictureID;
                if (currentDisplayedPicture.AUList.Count != 0)
                {
                     //RunRetrieval(currentDisplayedPicture.AUList);
                }
            }

            else
            {

                DisableButtons();
                pictureBoxImage.Image = null; 
            }
        }

        protected void AddAutoPicture(int au, Picture picture)
        {
            if (picture.AUList.Count == 0)
                picture.AUList = new List<int>();
            picture.AUList.Add(au);
            currentAUlistbox.Items.Add(au);
        }

        protected List<int> ShowNextImage()
        {
            if (newPictureDictionary.Count == 1)
            {
                firstpictureID = currentDisplayedPicture.PictureID;
                i = firstpictureID;
            }

           


            i++;

            if (i >= newPictureDictionary.Count + firstpictureID)
            {

                i = firstpictureID;
            }

            if (newPictureDictionary.Count != 0)
            {
               
                    //if DatabasepictureDictionary dont contains that index(picID) - repeat until find one that exists 
                    while (!newPictureDictionary.Keys.Contains(i))
                    {
                        i++;
                    }
                
                EnableButtons();
                pictureBoxImage.Image = newPictureDictionary[i].PictureImage;
                pictureBoxImage.SizeMode = PictureBoxSizeMode.Zoom;
                currentDisplayedPicture = newPictureDictionary[i];
                showCurrentAusinListbox();
                return newPictureDictionary[i].AUList;
            }
            else
            {
                DisableButtons();
                pictureBoxImage.Image = null;
                return null;
            }

        }

        protected List<int> ShowPreviousImage()
        {
            int lastpicturesid = 0;
            if (newPictureDictionary.Count == 1)
            {
                firstpictureID = currentDisplayedPicture.PictureID;
                i = firstpictureID;
                lastpicturesid = firstpictureID;
            }

            else
            {
                if (newPictureDictionary.Count != 0)
                {
                    lastpicturesid = newPictureDictionary.Values.Last().PictureID;
                }
            }

          

            i--;

            if (i < firstpictureID)
            {

                i = lastpicturesid;
            }

            if (newPictureDictionary.Count != 0)
            {
                
                    //if DatabasepictureDictionary dont contains that index(picID) - repeat until find one that exists 
                    while (!newPictureDictionary.Keys.Contains(i))
                    {
                        i--;
                    }
                
                EnableButtons();
                pictureBoxImage.Image = newPictureDictionary[i].PictureImage;
                pictureBoxImage.SizeMode = PictureBoxSizeMode.Zoom;
                currentDisplayedPicture = newPictureDictionary[i];
                showCurrentAusinListbox();
                return newPictureDictionary[i].AUList;
            }
            else
            {
                DisableButtons();
                pictureBoxImage.Image = null;
                return null;
            }

        }

        protected void fiilAUInsertListBox()
        {
            ReplyMsg reply = retrievalmsg.GetallAu();
            if (reply.MessageState)
            {
                List<int> aulist = (List<int>)reply.ParamArray[0];
                foreach (int au in aulist)
                    checkedListBoxAUList.Items.Add(au);
            }

        }

        protected void showCurrentAusinListbox()
        {
            currentAUlistbox.Items.Clear();
            if (currentDisplayedPicture.AUList.Count != 0)
            {
                foreach (int au in currentDisplayedPicture.AUList)
                {
                    currentAUlistbox.Items.Add(au);
                }
            }


        }

        protected void nextImageButton_Click(object sender, EventArgs e)
        {

            ShowNextImage();
        }

        protected void previousImageButton_Click(object sender, EventArgs e)
        {
            ShowPreviousImage();
        }

        protected void buttonAUListShow_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.CheckedListBox.CheckedItemCollection items = checkedListBoxAUList.CheckedItems;
            foreach (int au in items)
            {
                currentDisplayedPicture.AUList.Add(au);
            }
            showCurrentAusinListbox();
            retrievalmsg.AddAutoPictureinDB(currentDisplayedPicture);
            retrievalmsg.UpdateHasAUs(currentDisplayedPicture.PictureID, true);
        }

        protected void finishbutton_Click(object sender, EventArgs e)
        {
            
        }

        protected void buttonBack_Click(object sender, EventArgs e)
        {
            guiManager.SwitchForms(this, guiManager.mainMenuExpertGui);
        }

        protected void UpdateLastImages_Load(object sender, EventArgs e)
        {

        }

        private void buttonBack_Click_1(object sender, EventArgs e)
        {
            guiManager.SwitchForms(this, guiManager.mainMenuExpertGui);
            currentAUlistbox.Items.Clear();
            foreach (int i in checkedListBoxAUList.CheckedIndices)
            {
                checkedListBoxAUList.SetItemCheckState(i, CheckState.Unchecked);
            }

 

        }


        private void DisableButtons()
        {
            nextImageButton.Enabled = false;
            previousImageButton.Enabled = false;
            addButton.Enabled = false;
            
        }

         private void EnableButtons()
        {
            nextImageButton.Enabled = true;
            previousImageButton.Enabled = true;
            addButton.Enabled = true;
            
        }

         private void button1_Click(object sender, EventArgs e)
         {

         }

         private void buttonActionUnitsGuide_Click(object sender, EventArgs e)
         {
             Messages msg = new Messages();
             ReplyMsg envVar = msg.GetEnvironmentPath((int)GuiManager.Folders.Externals);
             msg.OpenFile("ActionUnitsGuide.pdf",(string) ((envVar.MessageState == true) ? envVar.ParamArray[0]: (MessageBox.Show("SysVar Exception"))));
         }

         private void currentAUlistbox_SelectedIndexChanged(object sender, EventArgs e)
         {

         }


    }


}
